package graph.day13;

import java.util.Arrays;

public class Leetcode_922 {

    public static int[] sortArrayByParityII(int[] nums) {
        //定义双指针
        int oddPoint = 1, evenPoint = 0;
        //开始移动并交换，最后一层必然为相互交换后再移动或者相同直接移动
        while (oddPoint < nums.length && evenPoint < nums.length) {
            //进行判断
            if (nums[oddPoint] % 2 == 0 && nums[evenPoint] % 2 == 1) {    //如果均不满足
                int temp = nums[oddPoint];
                nums[oddPoint] = nums[evenPoint];
                nums[evenPoint] = temp;
                oddPoint += 2;
                evenPoint += 2;
            } else if (nums[oddPoint] % 2 == 0 && nums[evenPoint] % 2 == 0) {  //偶数满足
                evenPoint += 2;
            } else if (nums[oddPoint] % 2 == 1 && nums[evenPoint] % 2 == 1) {  //奇数满足
                oddPoint += 2;
            } else {
                oddPoint += 2;
                evenPoint += 2;
            }
        }
        return nums;
    }

    public static void main(String[] args) {
        int[] nums = {4, 2, 5, 7};
        System.out.println(Arrays.toString(sortArrayByParityII(nums)));
    }
}
